//
// Created by chizuru on 2025/10/11.
//

#pragma once
#include <memory>
#include <atomic>
#include <thread>
#include <mutex>

class OperationResult;
class SerialPortAdapter;
class LoggingDecorator;
class ReconnectDecorator;
class DeviceComm;
class SensorTPDevice;
class SensorTPService;
class ConnectivityMonitor;
class SensorTPApplication {
public:
    explicit SensorTPApplication() = default;
    ~SensorTPApplication();
    OperationResult connect(const std::string& com);
    OperationResult isConnected();
    OperationResult close();
    OperationResult initialize();

    void startListening();
    void stopListening();

    const float getT() const;

    const float getP() const;

private:
    std::shared_ptr<SerialPortAdapter> adapter;
    std::shared_ptr<LoggingDecorator> loggingDecorator;
    std::shared_ptr<ReconnectDecorator> reconnectDecorator;
    std::shared_ptr<DeviceComm> comm;
    std::shared_ptr<SensorTPDevice> device;
    std::shared_ptr<SensorTPService> service;
    std::shared_ptr<ConnectivityMonitor> monitor;

    std::atomic<float> t;
    std::atomic<float> p;

    std::atomic_bool running{false};
    std::thread worker;
    std::chrono::milliseconds interval{10};
    std::mutex mtx;
    std::condition_variable cv;
};

